﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:IActivityMessagesManage
    /// </summary>
    public partial class ActivityMessagesManageDal: IActivityMessagesManage
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public ActivityMessagesManageDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int messageId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_ACTIVITY_MESSAGES");
            strSql.Append(" where MESSAGE_ID=@MESSAGE_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@MESSAGE_ID", SqlDbType.Int,4)
};
            parameters[0].Value = messageId;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ActivityMessagesManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into COFCO_ACTIVITY_MESSAGES(");
            strSql.Append("ACTIVITY_ID,MESSAGE_CONTENT,MEMBER_ID,IS_VIEW,MESSAGE_TIME)");
            strSql.Append(" values (");
            strSql.Append("@ACTIVITY_ID,@MESSAGE_CONTENT,@MEMBER_ID,@IS_VIEW,@MESSAGE_TIME)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4),
					new SqlParameter("@MESSAGE_CONTENT", SqlDbType.VarChar,800),
					new SqlParameter("@MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@IS_VIEW", SqlDbType.Int,4),
					new SqlParameter("@MESSAGE_TIME", SqlDbType.DateTime)};
            parameters[0].Value = model.Activityid;
            parameters[1].Value = model.Messagecontent;
            parameters[2].Value = model.Memberid;
            parameters[3].Value = model.Isview;
            parameters[4].Value = model.Messagetime;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ActivityMessagesManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_ACTIVITY_MESSAGES set ");
            strSql.Append("ACTIVITY_ID=@ACTIVITY_ID,");
            strSql.Append("MESSAGE_CONTENT=@MESSAGE_CONTENT,");
            strSql.Append("MEMBER_ID=@MEMBER_ID,");
            strSql.Append("IS_VIEW=@IS_VIEW,");
            strSql.Append("MESSAGE_TIME=@MESSAGE_TIME");
            strSql.Append(" where MESSAGE_ID=@MESSAGE_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4),
					new SqlParameter("@MESSAGE_CONTENT", SqlDbType.VarChar,800),
					new SqlParameter("@MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@IS_VIEW", SqlDbType.Int,4),
					new SqlParameter("@MESSAGE_TIME", SqlDbType.DateTime),
					new SqlParameter("@MESSAGE_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Activityid;
            parameters[1].Value = model.Messagecontent;
            parameters[2].Value = model.Memberid;
            parameters[3].Value = model.Isview;
            parameters[4].Value = model.Messagetime;
            parameters[5].Value = model.Messageid;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int messageId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_ACTIVITY_MESSAGES ");
            strSql.Append(" where MESSAGE_ID=@MESSAGE_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@MESSAGE_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = messageId;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string messageIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_ACTIVITY_MESSAGES ");
            strSql.Append(" where MESSAGE_ID in (" + messageIdlist + ")  ");
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null) > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ActivityMessagesManageModel GetModel(int messageId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 MESSAGE_ID,ACTIVITY_ID,MESSAGE_CONTENT,MEMBER_ID,IS_VIEW,MESSAGE_TIME from COFCO_ACTIVITY_MESSAGES         ");
            strSql.Append(" where MESSAGE_ID="+messageId);          
            ActivityMessagesManageModel model = new ActivityMessagesManageModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["MESSAGE_ID"].ToString() != "")
                {
                    model.Messageid = int.Parse(ds.Tables[0].Rows[0]["MESSAGE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ACTIVITY_ID"].ToString() != "")
                {
                    model.Activityid = int.Parse(ds.Tables[0].Rows[0]["ACTIVITY_ID"].ToString());
                }
                model.Messagecontent = ds.Tables[0].Rows[0]["MESSAGE_CONTENT"].ToString();
                if (ds.Tables[0].Rows[0]["MEMBER_ID"].ToString() != "")
                {
                    model.Memberid = int.Parse(ds.Tables[0].Rows[0]["MEMBER_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IS_VIEW"].ToString() != "")
                {
                    model.Isview = int.Parse(ds.Tables[0].Rows[0]["IS_VIEW"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MESSAGE_TIME"].ToString() != "")
                {
                    model.Messagetime = DateTime.Parse(ds.Tables[0].Rows[0]["MESSAGE_TIME"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select MESSAGE_ID,ACTIVITY_ID,MESSAGE_CONTENT,MEMBER_ID,IS_VIEW,MESSAGE_TIME ");
            strSql.Append(" FROM COFCO_ACTIVITY_MESSAGES ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (strWhere != null && !(strWhere.IndexOf("order") > -1))
            {
                strSql.Append(" order by MESSAGE_TIME desc"); 
            }
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" MESSAGE_ID,ACTIVITY_ID,MESSAGE_CONTENT,MEMBER_ID,IS_VIEW,MESSAGE_TIME ");
            strSql.Append(" FROM COFCO_ACTIVITY_MESSAGES ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_ACTIVITY_MESSAGES";
            parameters[1].Value = "MESSAGE_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

